import { defineStore } from 'pinia';
import service from '/@/utils/request';
import { Session } from '/@/utils/storage';

export interface LoginData {
	username: string;
	password: string;
	code: string;
	loginType: 'account' | 'wxid';
}

export const useAuth = defineStore('auth', {
	state: (): Auth => {
		return {
			token: null,
		};
	},
	actions: {
		async login(data: LoginData) {
			return service.post('/api/auth/admin/login', data).then((res) => {
				console.log('login', res);
				// eslint-disable-next-line no-console
				if (res.success) {
					const token = res.data.token;
					Session.set('token', token);
					return res;
				} else {
					throw new Error(res.message);
				}
			});
		},
	},
});
